import modelExtend from 'dva-model-extend'
import { query,detail } from './services/payOrderService'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';

export default modelExtend(lwjPageModel, {
  namespace: 'payOrderList',
  state: {
    currentItem: {},
    selectRows: [],
    selectedRowKeys: [],
    pageOptions: {
      sort: 'createTime',
      order: 'desc'
    },
    isExpand: false,
    visible: false,
    detailVisible: false,
    queryParam: {},
    currentType:"SALES"
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
      if(pathname==="/payorder/payorderlist"){
          dispatch({ type: 'query', payload: {} })
        }
      })
    }
  },
  effects: {
    * detail({
      payload
    }, { select, call, put }) {
      const data = yield call(detail, payload);
      yield put({
        type: 'setCurrentItem',
        payload: data.result
      })
    },
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ payOrderList }) => payOrderList.pageOptions);
      const queryParam = yield select(({ payOrderList }) => payOrderList.queryParam);
      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam,

        }
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'freshState',
        payload: {

        }
      })
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },


  },
  reducers: {
    onSelectChange(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    setCurrentTab(state, { payload }) {
      return {
        ...state,
        currentType: payload
      }
    },
    freshState(state, { payload }) {
      return {
        ...state,
        selectRows: [],
        selectedRowKeys: []
      }
    },
    setOptionType(state, { payload }) {
      return {
        ...state,
        optionType: payload
      }
    },
    setDetailVisible(state, { payload }) {
      return {
        ...state,
        detailVisible: payload
      }
    },
    setCurrentItem(state, { payload }) {
      return {
        ...state,
        currentItem: payload
      }
    },
    closeAllModal(state, { payload }) {
      return {
        ...state,
        auditVisible: false,
        detailVisible: false
      }
    },
  },
})
